<template>
  <div>
    <!--  面包屑导航区域 :to="{path:'/admin/welcome' 路由跳转 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{path:'/welcome'}">首页</el-breadcrumb-item>
      <el-breadcrumb-item>业务管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-row :gutter="25">
      <el-col :span="12">
        <!--  卡片视图区域  -->
        <el-card class="box-card">
          <el-button type="primary" @click=add1()  icon="el-icon-folder-add">新增分类</el-button>
          <el-button type="info" icon="el-icon-refresh" @click="refresh()">刷新</el-button>
          <!--   分类列表区域   -->
          <!--   分类表格   -->
          <el-table  :data="level1" v-on:click="onclickLevel1()">id
            <!--   模板列-数据值    -->
            <el-table-column label="id" prop="id"></el-table-column>
            <el-table-column label="父id" prop="parent"></el-table-column>
            <el-table-column id="activename" label="名称" prop="name"></el-table-column>
            <el-table-column label="顺序" prop="sort"></el-table-column>
            <!--   模板列-操作区    -->
            <!--   模板列-操作区-修改和删除    -->
            <el-table-column label="操作" width="180px">
              <template slot-scope="scope">
                <el-button circle type="primary" @click="onclickLevel1(scope.row)" icon="el-icon-s-promotion" size="mini"></el-button>
                <el-button circle type="primary" @click="showEditcategory(scope.row.id)" icon="el-icon-edit" size="mini"></el-button>
                <el-button circle type="danger" @click="del(scope.row.id)" icon="el-icon-delete" size="mini"></el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
      <el-col :span="12">
        <!--  卡片视图区域  -->
        <el-card class="box-card">
          <el-button type="primary" @click=add2() icon="el-icon-folder-add">二级分类</el-button>
          <el-button type="info" icon="el-icon-refresh" @click="refresh()">刷新</el-button>
          <!--   分类列表区域   -->
          <!--   分类表格   -->
          <el-table  :data="level2" v-on:click="onclickLevel1(category)">
            <!--   模板列-数据值    -->
            <el-table-column label="id" prop="id"></el-table-column>
            <el-table-column label="父id" prop="parent"></el-table-column>
            <el-table-column label="名称" prop="name"></el-table-column>
            <el-table-column label="顺序" prop="sort"></el-table-column>
            <!--   模板列-操作区    -->
            <!--   模板列-操作区-修改和删除    -->
            <el-table-column label="操作" width="180px">
              <template slot-scope="scope">
                <el-button type="primary" @click="showEditcategory(scope.row.id)" icon="el-icon-edit" size="mini">修改</el-button>
                <el-button type="danger" @click="del(scope.row.id)" icon="el-icon-delete" size="mini">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
    </el-col>
    </el-row>
    <!--  添加模态框  -->
    <el-dialog
        title="添加分类信息"
        :visible.sync="saveDialogVisible"
        width="50%"
        @close = "saveDialogClosed">
      <!--   内容主体区域   -->
      <el-form :model="category" :rules="saveFormRules" ref="saveFormRef" label-width="110px">
          <el-form-item label="上级分类" >
            <el-tag>{{active.name}}</el-tag>
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model="category.name"></el-input>
          </el-form-item>
          <el-form-item label="顺序" prop="sort">
            <el-input v-model="category.sort"></el-input>
          </el-form-item>
      </el-form>
      <!--  底部按钮区域   -->
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="savecategory">保 存</el-button>
        <el-button @click="saveDialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
    <!--  修改模态框  -->
    <el-dialog
        title="修改分类信息"
        :visible.sync="editDialogVisible"
        width="50%"
        @close = "editDialogClosed">
      <!--   内容主体区域   -->
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="110px">
        <el-form-item label="id" prop="id">
          <el-input v-model="editForm.id"></el-input>
        </el-form-item>
        <el-form-item label="父id" prop="parent">
          <el-input v-model="editForm.parent"></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="顺序" prop="sort">
          <el-input v-model="editForm.sort"></el-input>
        </el-form-item>
      </el-form>
      <!--  底部按钮区域   -->
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editcategory">保 存</el-button>
        <el-button @click="editDialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
/*
* 方法 methods:
*   list()-分类列表查询/刷新
*   handleSizeChange 监听页面条数变化
*   handleCurrentChange 监听页码值变化
*   addDialogClosed 监听添加模态框的关闭事件
*   editDialogClosed 监听修改模态框的关闭事件
*   addcategory 分类添加
*   showEditcategory 修改分类前根据id获取分类对象决定是否展示模态框
*   changedStatus 状态封号
*   del 删除分类
*   openFullScreen() 等待Loading加载
*   refresh() 刷新
*
* 生命周期 mounted:
*   _this.list()-页面初始化时候执行list
*
* 数据 data:function ():
*   categorys:[]-一个category空数组
*   addDialogVisible-控制模态框显示隐藏的布尔值
*   total 总条数
*   queryInfo 请求参数块
*
* */

export default {
    name: "Category",
    data:function (){
      return{
        active:{},
        level1:[],
        level2:[],
        category:{},
        categorys:[],
        saveDialogVisible:false,
        editDialogVisible:false,
        saveFormRules:{
          name:[
            {required:true,message:'请输入分类名称',trigger:'blur'},
            {min:3,max:10,message:'请分类名称在3~10个字符之间',trigger: 'blur'}
          ],
        },
        editFormRules:{
          name:[
            {required:true,message:'请输入分类名称',trigger:'blur'},
            {min:3,max:10,message:'请分类名称在3~10个字符之间',trigger: 'blur'}
          ],
        },
        editForm:{}
      }
    },
    mounted:function (){
      let _this = this;
      _this.all();//组件加载时立刻执行一次查询渲染数据
    },
    methods:{
      add1(){
        let _this = this;
        _this.active = {};
        _this.level2 = [];
        _this.category = {
          parent: "00000000"
        };
        _this.saveDialogVisible = true;
      },
      add2(){
        let _this = this;
        if (Tool.isEmpty(_this.active)) {
          _this.$message.success('请先点击一级分类菜单');
          return;
        }
        _this.category = {
          parent: _this.active.id
        };
        _this.saveDialogVisible = true;
      },
      // 监听添加模态框的关闭事件
      saveDialogClosed(){
        let _this = this;
        _this.$refs.saveFormRef.resetFields()
      },
      // 监听添加模态框的关闭事件
      editDialogClosed(){
        let _this = this;
        _this.$refs.editFormRef.resetFields()
      },
      // 查询
      all(){
        let _this = this;
        _this.$ajax.post(process.env.VUE_APP_SERVER+'/business/admin/category/all',{
        }).then((response)=>{
          console.log("查询分类列表结果:",response);
          let resp = response.data;
          _this.categorys = resp.content;
          // 将所有记录格式化成树形结构
          _this.level1 = [];
          for (let i = 0; i < _this.categorys.length; i++) {
            let c = _this.categorys[i];
            if (c.parent === '00000000') {
              _this.level1.push(c);
              for (let j = 0; j < _this.categorys.length; j++) {
                let child = _this.categorys[j];
                if (child.parent === c.id) {
                  if (Tool.isEmpty(c.children)) {
                    c.children = [];
                  }
                  c.children.push(child);
                }
              }
            }
          }
        })
      },
      savecategory(){
        let _this = this;
        _this.$refs.saveFormRef.validate(vaild=>{
          if(!vaild)return //如果验证通过才发起添加用户的网络请求
          _this.$ajax.post(process.env.VUE_APP_SERVER+'/business/admin/category/save',_this.category).then((response)=>{
            console.log("保存分类列表结果",response);
            let resp = response.data;
            if (resp.success){
              _this.$data.saveDialogVisible = false;
              _this.$data.editDialogVisible = false;
              _this.all();
              _this.$message.success('保存分类信息成功');
            }
          })
        })
      },
      editcategory(page){
        let _this = this;
        _this.$refs.editFormRef.validate(vaild=>{
          if(!vaild)return //如果验证通过才发起添加用户的网络请求
          _this.$ajax.post(process.env.VUE_APP_SERVER+'/business/admin/category/save',_this.editForm).then((response)=>{
            console.log("修改分类列表结果",response);
            let resp = response.data;
            if (resp.success){
              _this.$data.saveDialogVisible = false;
              _this.$data.editDialogVisible = false;
              _this.all();
              _this.$message.success('修改分类信息成功');
            }
          })
        })
      },
      showEditcategory(id){
        console.log("当前行id值",id);
        let _this = this;
        _this.$ajax.get(process.env.VUE_APP_SERVER+'/business/admin/category/selectId/'+id).then((response)=>{
          let resp = response.data;
          if(response.status !== 200){
            return this.$message.error("查询分类信息失败");
          }
          _this.editForm = resp.content;
          console.log("ID查询分类列表结果:",response);
          _this.editDialogVisible = true;
        })
      },
      changedStatus(category){
        let _this = this;
        console.log("测试",category)
        _this.$ajax.post(process.env.VUE_APP_SERVER+'/business/admin/category/changedStatus',category).then((response)=>{
          if(response.status !=200){
            return _this.$message.error("修改状态失败")
          }
          _this.$message.success("修改状态成功")
          console.log("改变分类状态结果",response);
            let resp = response.data;
        })
      },
      del(id){
        this.$confirm('此操作将分类永久删除, 是否继续?', '删除提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          console.log("删除当前的id",id)
          let _this = this;
          _this.openFullScreen();
          _this.$ajax.delete(process.env.VUE_APP_SERVER+'/business/admin/category/delete/'+id).then((response)=>{
            console.log("删除分类列表结果:",response);
            let resp = response.data;
            if (resp.success){
              _this.all();
              _this.$message.success("删除分类成功")
            }
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
            });
          });
        },
      openFullScreen() {
        const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        setTimeout(() => {
          loading.close();
        }, 1000);
      },
      refresh(){
        this.openFullScreen();
        if(!document.getElementById('queryName')==""){
          this.selectByName();
        }else{
          this.all();//重新获取查询all,达到分页切换页面前进后退
        }
      },
      onclickLevel1(row){
        let _this = this;
        _this.active = row;
        _this.level2 = row.children;
      }
    },
}
</script>

<style lang="less" scoped>
.el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
}
.el-icon-arrow-down {
  font-size: 12px;
}
.activename{
  font-style: oblique !important;
}
</style>